﻿using System;
using System.Collections.Generic;
using VideoStore.Business.Facade;
using VideoStore.Domain.Entities;
using VideoStore.Services.Dto;
using VideoStore.Services.Interface;
using VideoStore.Common.Response;
using VideoStore.Services.Translation;
using VideoStore.Services.Translation.Base;
using VideoStore.Services.Translation.Factory;
using VideoStore.Common.Response.Extension;

namespace VideoStore.Services
{
    public class CatalogueService : ICatalogueService
    {
        #region Implementation of ICatalogueService

        /// <summary>
        /// Creates the video.
        /// </summary>
        /// <param name="newVideo">The new video.</param>
        /// <returns></returns>
        public Response<CatalogueDto> CreateVideo(CatalogueDto newVideo)
        {
            var response = new Response<CatalogueDto>();
            response.IsSuccessful = true;
            try
            {
                var facade = VideoStoreFacade.Instance;
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(CatalogueTranslator));
                var entity = translator.TranslateToEntity<CatalogueDto, Catalogue>(newVideo);
                Catalogue savedEntity = facade.CreateVideo(entity);
                response.ResponseObject = translator.TranslateToDto<Catalogue, CatalogueDto>(savedEntity);
                response.Messages.AddSuccessfulSaveMessage("Video");
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        /// <summary>
        /// Gets the video by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Response<CatalogueDto> GetVideoById(int id)
        {
            var response = new Response<CatalogueDto>();
            response.IsSuccessful = true;
            try
            {
                var facade = VideoStoreFacade.Instance;
                Catalogue entity = facade.GetVideoById(id);
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(CatalogueTranslator));
                response.ResponseObject = translator.TranslateToDto<Catalogue, CatalogueDto>(entity);
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        /// <summary>
        /// Gets the video by catalogue number.
        /// </summary>
        /// <param name="catalogueNumber">The catalogue number.</param>
        /// <returns></returns>
        public Response<CatalogueDto> GetVideoByCatalogueNumber(string catalogueNumber)
        {
            var response = new Response<CatalogueDto>();
            response.IsSuccessful = true;
            try
            {
                var facade = VideoStoreFacade.Instance;
                Catalogue entity = facade.GetVideoByCatalogueNumber(catalogueNumber);
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(CatalogueTranslator));
                response.ResponseObject = translator.TranslateToDto<Catalogue, CatalogueDto>(entity);
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        /// <summary>
        /// Gets the video by director.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public Response<List<CatalogueDto>> GetVideosByDirector(string name)
        {
            var response = new Response<List<CatalogueDto>>();
            response.ResponseObject = new List<CatalogueDto>();
            response.IsSuccessful = true;
            try
            {
                var facade = VideoStoreFacade.Instance;
                IEnumerable<Catalogue> entities = facade.GetVideosByDirector(name);
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(CatalogueTranslator));
                foreach (var entity in entities)
                {
                    var dto = translator.TranslateToDto<Catalogue, CatalogueDto>(entity);
                    response.ResponseObject.Add(dto);
                }
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        /// <summary>
        /// Gets the video by genre.
        /// </summary>
        /// <param name="genreName">Name of the genre.</param>
        /// <returns></returns>
        public Response<List<CatalogueDto>> GetVideosByGenre(string genreName)
        {
            var response = new Response<List<CatalogueDto>>();
            response.ResponseObject = new List<CatalogueDto>();
            response.IsSuccessful = true;
            try
            {
                var facade = VideoStoreFacade.Instance;
                IEnumerable<Catalogue> entities = facade.GetVideoByGenre(genreName);
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(CatalogueTranslator));
                foreach (var entity in entities)
                {
                    var dto = translator.TranslateToDto<Catalogue, CatalogueDto>(entity);
                    response.ResponseObject.Add(dto);
                }
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.Errors.AddFatalError(ex.Message);
            }
            return response;
        }

        #endregion
    }
}